Chapter 6, "Defining Classes and Objects," describes how to specialize your classes to allow for comparison of objects.
isComparable
and the cmp
global function. The isComparable
generic function, defined by RootObject
, is one of the four generics that are the basis of the Comparison protocol. isComparable
returns true
or false
, depending on whether its arguments are comparable. If isComparable
returns true
, then comparison operators and comparison functions (described in Chapter 2, "ScriptX Building Blocks") can be used on the tested objects without causing an exception. isComparable 1 2
true
isComparable 1 "banana"
false
The cmp
global function is a general purpose comparison function. When it is called with two arguments (which must be comparable), it uses the equal
( =
) and lt
( <
) generic functions to return one of three values:
@same
if the arguments have the same values ( equal
evaluates to true
)
@before
if the first argument is less than the second ( lt
returns true
)
@after
if the first argument is greater than the second ( lt
returns false
)
cmp
function:cmp 1 1
@same
cmp 1 4
@before
cmp 4 1
@after
Universal comparison functions can compare two objects of different classes. If two objects are of the same class, and that class does not implement a method for localLT
, then ult
can still report an exception. Generally, all objects for which comparison is meaningful, such as numbers and strings, implement a method for localLT
. To insure that the universal comparison functions work for a given object, use canObjectDo
to query the object to see whether it implements localLT
. For information on canObjectDo
, see page 72.
The seven universal comparison functions are ueq
, une
, ult
, ugt
, uge
, ule
, and ucmp
.
The functions ueq
(universal equal) and une
(universal not equal) test for object equality. The ueq
function returns true
only if:
une
returns true
if either the classes of its arguments are not the same or the values of its arguments are not the same.arg1 := 1.9 -- an instance of ImmediateFloat
arg2 := 1.9 as Float
arg1 = arg2 -- equivalent to the equal global function
true
ueq arg1 arg2 -- test for universal equality
false
In this example, arg1
is assigned the number 1.9 (an instance of class ImmediateFloat
), and arg2
is assigned the same value, coerced to the Float
class. Because the values of these numbers are the same, the equality test returns true
. However, because the classes of these objects are different, ueq
returns false
.
The global functions ugt
, ult
, uge
, and ule
are used for universal comparisons of magnitude. (They are the universal counterparts of the >
(gt
), <
(lt
), >=
(ge
), and <=
(le
) operations, respectively). Each of these functions returns true
if one of the following rules is true:
ult 1 2
, the classes are the same (ImmediateInteger
), and the value 1
is indeed less than 2
, so the function returns true
. Since lt 1 2
returns true
, ult 1 2
returns true
.
ult 1 "this"
, the two classes (ImmediateInteger
and String
) are different, so the names of the classes are compared. Because ImmediateInteger
is alphabetically "less than" String
(that is, "I" comes before "S"), the expression returns true
.
Finally, the ucmp
function is the universal equivalent of the cmp
function. ucmp
returns one of the following values, based on the following rules:
@same
if the ueq
function returns true
for the arguments.
@before
if the ult
function returns true
for the arguments.
@after
if the ueq
and ult
functions both return false
for the arguments.
This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.